package com.sinosoft.weixin.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * Created by huzhengying on 2017/11/18.
 */
public class RandomUtil {
    public static String getRandomNum(int index){
        StringBuffer numLength = new StringBuffer("1");
        for(int i = 0;i < index;i++){
            numLength.append("0");
        }

        //int类型不允许超过10，不满10位的随机数左补0
        String realNumLength = null;
        if(numLength.length() > 10){
            realNumLength = numLength.substring(0,10);
        }else {
            realNumLength = numLength.toString();
        }

        String randomNum = createRandomNum(Integer.valueOf(realNumLength.toString()));
        randomNum = StringUtils.leftPad(randomNum,numLength.length()-1,"0");
        return randomNum;
    }

    private static String createRandomNum(int length){
        int randomNum = (int) (Math.random() * length);
        return String.valueOf(randomNum);
    }
}
